*** Fig 3, B2, B3 -- Selection from Origin
use "$path\Tempfiles\CommunistCensus_vars.dta", clear
svyset serial [pweight=perwt], strata(new_strata) singleunit(scaled)
keep if year >= 2010 // use censuses closest to 2011
cd "$path\Output\Communist"

* Poland
putexcel set poland_all, replace

svy: tab isced_int if birthyr >= 1946 & birthyr <= 1986 & country == 616  //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if birthyr >= 1925 & birthyr <= 1945 & country == 616 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if birthyr >= 1942 & birthyr <= 1962 & country == 616  //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if birthyr >= 1948 & birthyr <= 1968 & country == 616 //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Hungary
putexcel set hungary_all, replace

svy: tab isced_int if birthyr >= 1946 & birthyr <= 1986 & country == 348  //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if birthyr >= 1925 & birthyr <= 1945 & country == 348 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if birthyr >= 1942 & birthyr <= 1962 & country == 348  //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if birthyr >= 1948 & birthyr <= 1968 & country == 348  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Romania
putexcel set romania_all, replace

svy: tab isced_int if birthyr >= 1946 & birthyr <= 1986 & country == 642 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if  birthyr >= 1925 & birthyr <= 1945 & country == 642 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if  birthyr >= 1942 & birthyr <= 1962 & country == 642 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if birthyr >= 1948 & birthyr <= 1968 & country == 642 //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Romanian Jews
putexcel set romania_jews, replace

svy: tab isced_int if birthyr >= 1946 & birthyr <= 1986 & country == 642 & jewish == 1 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if  birthyr >= 1925 & birthyr <= 1945 & country == 642 & jewish == 1 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if  birthyr >= 1942 & birthyr <= 1962 & country == 642 & jewish == 1 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if birthyr >= 1948 & birthyr <= 1968 & country == 642 & jewish == 1 //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Romanian Germans
putexcel set romania_germans, replace

svy: tab isced_int if birthyr >= 1946 & birthyr <= 1986 & country == 642 & german == 1 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if  birthyr >= 1925 & birthyr <= 1945 & country == 642 & german == 1 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if  birthyr >= 1942 & birthyr <= 1962 & country == 642 & german == 1 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if birthyr >= 1948 & birthyr <= 1968 & country == 642 & german == 1 //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Russia 2010 census
putexcel set russia_all, replace

svy: tab isced_rus if birthyr >= 1946 & birthyr <= 1986 & country == 643 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if birthyr >= 1925 & birthyr <= 1945 & country == 643  //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if birthyr >= 1942 & birthyr <= 1962 & country == 643 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if birthyr >= 1948 & birthyr <= 1968 & country == 643 //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Russia 2010 census -- Jews
putexcel set russia_jews, replace

svy: tab isced_rus if birthyr >= 1946 & birthyr <= 1986 & country == 643 & jewish == 1 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if birthyr >= 1925 & birthyr <= 1945 & country == 643 & jewish == 1  //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if birthyr >= 1942 & birthyr <= 1962 & country == 643 & jewish == 1  //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if birthyr >= 1948 & birthyr <= 1968 & country == 643 & jewish == 1  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

* Russia 2010 census -- Germans
putexcel set russia_germans, replace

svy: tab isced_rus if birthyr >= 1946 & birthyr <= 1986 & country == 643 & german == 1 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if birthyr >= 1925 & birthyr <= 1945 & country == 643 & german == 1  //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if birthyr >= 1942 & birthyr <= 1962 & country == 643 & german == 1  //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if birthyr >= 1948 & birthyr <= 1968 & country == 643 & german == 1  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)


*** Tab B3 -- Mincer
use "$path\Tempfiles\CommunistCensus_vars.dta", clear
svyset serial [pweight=perwt], strata(new_strata) singleunit(scaled)

*gen controls
gen age_bin = floor(age/5)*5 //5-yr bins
gen exper = age - edu_years - 6
gen exper2 = exper*exper

global controls_out exper exper2 female i.year
	
*select sample
drop sample
gen sample = (age >= 25 & age <= 65)
drop if age == .	
	
** Hungary 1970, 1980, 1990
preserve
keep if country == 348 & inlist(year,1970,1980,1990)

** returns to education
local append_replace replace
foreach group in population minority{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_pre.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_hun_pre.txt"

restore

** Hungary 2001, 2011

preserve
keep if country == 348 & inlist(year,2001,2011)

** returns to education
local append_replace replace
foreach group in population{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_post.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_hun_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_hun_post.txt"

restore

** Poland 1978, 1988

preserve
keep if country == 616 & inlist(year,1978,1988)

** returns to education
local append_replace replace
foreach group in population{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_pre.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_pol_pre.txt"

restore

** Poland 2002

preserve
keep if country == 616 & year == 2002

** returns to education
local append_replace replace
foreach group in population minority{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_post.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_pol_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_pol_post.txt"

restore

** Romania 1977, 1992

preserve
keep if country == 642 & inlist(year,1977,1992)

** returns to education
local append_replace replace
foreach group in population minority{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_pre.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_pre.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_rom_pre.txt"

restore


** Romania 2002, 2011

preserve
keep if country == 642 & inlist(year,2002,2011)

** returns to education
local append_replace replace
foreach group in population minority{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_post.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	svy, subpop(sample if `group' == 1): reg high_med_occ edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rom_post.xls", append label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_rom_post.txt"

restore

** Russia 2002, 2010

preserve
keep if country == 643 & inlist(year,2002,2010)

** returns to education
local append_replace replace
foreach group in population minority{
	svy, subpop(sample if `group' == 1): reg employed edu_years $controls_out
	outreg2 using "$path\Output\Communist\mincer_rus_post.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\Communist\mincer_rus_post.txt"

restore
